// require('dotenv').config()

const koa = require('koa')
const logger = require('koa-logger')
const views = require('koa-view')
const serve = require('koa-static')
const bodyParser = require('koa-bodyparser')
const session = require('koa-session2')
const validate = require('koa-validate')
const mongoose = require('mongoose')
const cors = require('koa-cors')
const pageNotFound = require('./src/middlewares/404')
const appError = require('./src/middlewares/app-error')
const public = require('./src/api/public')
const v1 = require('./src/api/v1')
// const RedisStore = require('./src/models/RedisStore')

mongoose.connect(process.env.MONGODB_DATABASE)
mongoose.Promise = global.Promise

const app = koa()
const PORT = 3333

app.use(cors({
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
}))

validate(app)

app.use(logger())

// app.use(session({
//   store: new RedisStore
// }))

app.use(serve(__dirname + '/src/statics'))

app.use(views(__dirname + '/src/views'))

app.use(bodyParser())

app.use(appError)

app.use(public.routes())
app.use(v1.routes())

app.use(pageNotFound)

app.listen(PORT, () => {
  console.log(`Server is running at port ${PORT}`)
})